import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'

// create an axios instance

const config = {
	baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
	// withCredentials: true, // send cookies when cross-domain requests
	timeout: 5000 // request timeout
};
const postService = axios.create(config);


postService.interceptors.request.use(
	function(config) {
		// Do something before request is sent
		if (config.method === 'post') {
			config.headers.post['Content-Type'] = 'multipart/form-data';
			let formdata = new FormData();
			Object.keys(config.data).forEach(key=>{
				const obj = config.data[key];
				if(typeof(obj)=="object"){
					Object.keys(obj).forEach(kk=>{
						formdata.append(key,obj[kk]);
					});
				}else{
					formdata.append(key,obj);
				}
			});
			config.data = formdata;
		}
		return config;
	},
	function(error) {
		// Do something with request error
		return Promise.reject(error);
	}
);
postService.interceptors.response.use(
	function(response) {
		// Do something with response data
		return response;
	},
	function(error) {
		// Do something with response error
		return Promise.reject(error);
	}
);

export default postService
